package com.controller;

import com.entity.CalendarEntity;
import com.mapper.CalendarMapper;
import com.util.HttpConnectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class CalendarContrller {
    @Autowired
    private CalendarMapper calendarMapper;

    @RequestMapping("/day/isweekday")
    public String isweekday(HttpServletRequest request, HttpServletResponse response) {
        String date = request.getParameter("date");
        boolean isweekday = HttpConnectUtil.isWeekday(date);
        return isweekday+"";
    }
    @RequestMapping("/data/getDataToMysql")
    public String getDataToMysql(HttpServletRequest request, HttpServletResponse response) {
        //遍历获取所有节假日 和调休日
        for (int i = 2; i < 12; i+=3) {
            System.out.println(i);
            String date="2018-"+i;
            HttpConnectUtil.getHolidaysAndWeekdays(date);
        }
        //遍历所有天数插入数据库
        for (int i = 1; i <= 12; i++) {
            List<String> dateList = HttpConnectUtil.dayReport(2018, i);
            for (String date : dateList) {
                boolean isWeekday = HttpConnectUtil.isWeekday(date);
                CalendarEntity calendarEntity = new CalendarEntity();
                calendarEntity.setDate(date);
                calendarEntity.setYear("2018");
                calendarEntity.setMonth(i+"");
                calendarEntity.setIsweekday(isWeekday+"");
                calendarMapper.insert(calendarEntity);
            }
        }
        return "ok";
    }

    @RequestMapping("/month/getWeekDayCountByMonth")
    public String getWeekDayCountByMonth(HttpServletRequest request, HttpServletResponse response) {
        String month = request.getParameter("month");
        int weekDayCount = calendarMapper.getWeekDayCountByMonth(month);
        return weekDayCount+"zz";
    }
}
